﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using System.Collections;

namespace SkypeConsole
{
    class WolframAPI
    {
        public static WebClient client;

        public WolframAPI()
        {
            client = new WebClient();
        }

        public ArrayList query(string query)
        {
            ArrayList result = new ArrayList();

            try
            {
                string data = client.DownloadString("http://api.wolframalpha.com/v2/query?appid=76RJL3-6662G93VA9&input=" + query + "&format=plaintext");

                XmlReader reader = XmlReader.Create(new StringReader(data));
                while (reader.ReadToFollowing("pod"))
                {
                    reader.MoveToFirstAttribute();
                    result.Add(reader.Value);
                    reader.ReadToFollowing("plaintext");
                    result.Add(reader.ReadElementContentAsString());
                }

                return result;
            }
            catch (Exception e)
            {
                result.Add(e.ToString());
                return result;
            }
        }

        public string format(ArrayList list)
        {
            string result = "";

            if (list.Count == 1) return result;

            for (int i = 0; i < list.Count; i += 2)
            {
                result += "/// " + list[i] + "\n";
                result += list[i + 1] + "\n";
                result += "---\n";
            }

            return result;
        }
    }
}
